package com.twu.biblioteca;

import sun.rmi.runtime.Log;

import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.List;


public class LoginManager {
    ArrayList<Login> userLoginDataBase;
    Library library;
    String currentUser;
    Role currentRole;

    LoginManager(Library library) {
        userLoginDataBase = new ArrayList<Login>();
        //userLoginDataBase.add(new Login("999-0000", "111", new UserRole());
        userLoginDataBase.add(new Login("111-2222", "222", new UserRole(library, this)));
        userLoginDataBase.add(new Login("999-0000", "222", new UserRole(library, this)));
        userLoginDataBase.add(new Login("000-0000", "222", new LibrarianRole(library, this)));
        this.library = library;
        currentUser = null;
        currentRole = null;
    }

    public void userLogin(String username, String password) throws UnAuthorizedUser {
        for (Login login : userLoginDataBase)
            if (login.username.matches(username))
                if (login.password.matches(password)) {
                    currentUser = username;
                    currentRole = login.role;
                    return;
                }


        throw new UnAuthorizedUser();

    }

    public boolean isUserLoggedIn() {
        return currentUser != null;
    }

    public void logOut() {
        currentRole = null;
        currentUser = null;
    }
}
